import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/styles/index.dart';
import 'package:go_router/go_router.dart';

class ModalUtil {
  //底部弹出
  static showBottomModal({
    required BuildContext context,
    // required Widget child,
    required Widget Function(StateSetter setState) child,
    Color? barrierColor,
    bool isDismissible = true,
    bool enableDrag = true,
    bool isScrollControlled = true,
    Color? backgroundColor,
    double? paddingTop,
    double? radius,
    Function()? callback
  }) async {
    await showModalBottomSheet(
      context: context,
      isDismissible: isDismissible,
      isScrollControlled: isScrollControlled,
      enableDrag: enableDrag,
      barrierColor: barrierColor ?? Colors.black54,
      backgroundColor: backgroundColor,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(radius ?? 60.w),
          topRight: Radius.circular(radius ?? 60.w)
        )
      ),
      builder: (modalContext) {
          return StatefulBuilder(builder: (context1, state) {
            return Container(
              padding: EdgeInsets.only(top: paddingTop??0),
              child: ClipRRect(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(radius ?? 60.w),
                    topRight:Radius.circular(radius ?? 60.w)
                ),
                child: child(state),
              ),
            );
          });
        });
    if (callback != null) {
      callback();
    }
  }

  static showCustomDialog({
    required BuildContext context,
    required String title,
    required void Function() confirmHandle,
    void Function()? cancelHandle,
    Widget? subTitle,
    String cancelTxt = '取消',
    String confirmTxt = '确定',
  }) {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(title),
            content: subTitle,
            actions: <Widget>[
              CupertinoDialogAction(child: Text(cancelTxt, style: const TextStyle(color: Colors.grey)),onPressed: (){
                if (cancelHandle != null) {
                  cancelHandle();
                }
                context.pop();
              }),
              CupertinoDialogAction(onPressed: confirmHandle, child: Text(confirmTxt, style: TextStyle(color: ColorStyle.red))),
            ],
          );
        });
  }

  static Future<T?> showGeneralDialogWidget<T>({
    required BuildContext context,
    required Widget modalWidget,
    Color? barrierColor
  }) async {
    return showGeneralDialog(
        context: context,
        barrierDismissible: false,
        barrierLabel: '',
        barrierColor: barrierColor ?? Colors.transparent,
        transitionDuration: const Duration(milliseconds: 200),
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
          return modalWidget;
        });
  }

  ///模糊背景弹窗
  static Future<T?> showBlurGeneralDialogWidget<T>({
    required BuildContext context,
    required Widget modalWidget,
    Color? barrierColor,
    bool? barrierDismissible
  }) async {
    return showGeneralDialog(
        context: context,
        barrierDismissible: barrierDismissible ?? false,
        barrierLabel: '',
        barrierColor: barrierColor ?? Colors.transparent,
        transitionDuration: const Duration(milliseconds: 200),
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
          return BackdropFilter(
            filter: ImageFilter.blur(
              sigmaX: 10, // 横向模糊程度
              sigmaY: 10, // 纵向模糊程度
            ),
            child: modalWidget
          );
        });
  }
}